import numpy as np
import string
import os
# from gensim import corpora
from baseline.DevRec import Pretreatment
import util.PATH as PATH
import util.data_helper as data_helper

# 新建LDA模型
lda = Pretreatment.LDA()
# Corpus ,Product,Component,Developer= lda.read_csv(dir="AspectJ.csv",summary=2,description=3,product = 4,component=5,assigned_to= 6,comment=7)
# Corpus, Product, Component, Developer = lda.read_csv(dir="/document/Bug_msg/PartFive/GCC/commenter_bug_msg", summary=2,
#                                                      description=3, product=4, component=5, assigned_to=6, comment=7)

bug_msg_all, sorted_bug_msg = data_helper.get_msg_all()
Corpus = []
Product = []
Component = []
Developer = []
for i in range(len(sorted_bug_msg)):
	c_bug = sorted_bug_msg[i]
	bugid = c_bug[0]
	bug_value = c_bug[1]
	words = []
	if not os.path.exists(PATH.path_corpus + str(bugid)):
		continue
	with open(PATH.path_corpus + str(bugid), 'r') as reader:
		for line in reader.readlines():
			words.append(line.strip())
	Corpus.append(words)
	Product.append(bug_value[3])
	Component.append(bug_value[4])
	Developer.append(bug_value[0].strip().split(' '))

Terms, Topics = lda.build_from_corpus(Corpus)  # terms为训练集的特征集表示，topic为doc-topic分布

# 生成MLkNN
mlknn = Pretreatment.BR_MLkNN()
Terms = mlknn.makematrix(Terms, lenth=len(lda.dictionary))
Topics = mlknn.makematrix(Topics, lenth=lda.num_topics)
X, y = mlknn.make_Xy(Terms=Terms, Topics=Topics, Product=Product, Component=Component, Developer=Developer)

step = int(len(X) / 11)
# 分11组验证
# 仅br分析的结果
for n in range(step, len(X) - step, step):
	testX_Validation = X[n:n + step, :]
	testy_Validation = y[n:n + step, :]
	trainX_Validation = X[0:n, :]
	trainy_Validation = y[0:n, :]
	print("test only br-analysis")
	mlknn.br_mlknn.fit(trainX_Validation, trainy_Validation)
	score = mlknn.br_mlknn.predict(testX_Validation)

	r5 = mlknn.recall(y=testy_Validation, score=score, n=5)
	r10 = mlknn.recall(y=testy_Validation, score=score, n=10)
	print('r5:', r5)
	print('r10:', r10)

# 双重分析的结果，如果设置gama[0]=0,则为仅分析开发者
for n in range(step, len(X) - step, step):
	testX_Validation = X[n:n + step, :]
	testy_Validation = y[n:n + step, :]
	trainX_Validation = X[0:n, :]
	trainy_Validation = y[0:n, :]
	r5 = 0
	r10 = 0
	if n == step:
		mlknn.findgama(testX=trainX_Validation, testy=trainy_Validation, X=X, y=y)
	else:
		mlknn.set(trainX_Validation, trainy_Validation)
		r5 = mlknn.test(testX_Validation, testy_Validation, recall=5)
		r10 = mlknn.test(testX_Validation, testy_Validation, recall=10)
	print('r5:', r5)
	print('r10:', r10)
